﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerCreator.LMS.Common.Script;

public partial class Theme_3_ShowSkill : PowerCreator.LMS.Web.Base.BasePage
{
    public string _contentID = "0";
    public string _type = "0";
    PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected void Page_Load(object sender, EventArgs e)
    {
        InitPage();
        if (Request.QueryString["ContentID"] != null)
            _contentID = Request.QueryString["ContentID"].ToString();
        if (Request.QueryString["typeID"] != null)
            _type = Request.QueryString["typeID"].ToString();
        ViewState["ContentID"] = _contentID;
        ViewState["typeID"] = _type;
        if (!IsPostBack)
        {
            BinderPage();
            BinderForm();
        }
    }
    protected override void InitPage()
    {
        btnSave.Text = GetLanageValueByKey("theme_Comment_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnCloseMe.Value = GetLanageValueByKey("theme_Comment_Cancel", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderPage()
    {
        //if(Session["UserID"]!=null)
        //{
        //    string uid = Session["UserID"].ToString();

        //}
        DataTable dt = new DataTable();
        string sql = " FKSkillTypeId=" + _type;
        dt = op.QuerySkill(sql);
        rptSkill.DataSource = dt;
        rptSkill.DataBind();
    }
    protected void BinderForm()
    {
        try
        {
            for (int i = 0; i < rptSkill.Items.Count; i++)
            {
                DropDownList ddl = (DropDownList)rptSkill.Items[i].FindControl("ddl_SkillScore");
                Label lbl_SkillScore = (Label)rptSkill.Items[i].FindControl("lbl_SkillScore");
                Label lbl_SkillID = (Label)rptSkill.Items[i].FindControl("lbl_SkillID");
                for (int m = int.Parse(lbl_SkillScore.Text); m >= 0; m--)
                {
                    ddl.Items.Add(new ListItem(m.ToString(), m.ToString()));
                }
                Binderddl(ddl, lbl_SkillID.Text, lbl_SkillScore.Text);
            }
        }
        catch
        {
            //Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
    }
    private void Binderddl(DropDownList ddl, string SkillID, string SkillScore)
    {
        int uid = GetCreatedByInfo().UserID;
        if (uid > 0)
        {
            string sql = " FKGradeSkillID=" + SkillID + " and FKGradeContentGuid='" + _contentID + "' and FKGradeUserID=" + uid.ToString();
            DataTable dt = new DataTable();
            dt = op.QueryGrade(sql);
            if (dt.Rows.Count > 0)
            {
                ddl.Items.FindByValue(dt.Rows[0]["GradeScore"].ToString()).Selected = true;
            }
        }
    }

    protected void ddl_onChange(object sender, System.EventArgs e)
    {
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        PowerCreator.LMS.Grade.Entity.VOTGrade vo = new PowerCreator.LMS.Grade.Entity.VOTGrade();

        int uid = GetCreatedByInfo().UserID;
        if (uid > 0)
        {

            try
            {
                for (int i = 0; i < rptSkill.Items.Count; i++)
                {
                    DropDownList ddl = (DropDownList)rptSkill.Items[i].FindControl("ddl_SkillScore");
                    //Label lbl_SkillScore = (Label)rptSkill.Items[i].FindControl("lbl_SkillScore");
                    Label lbl_SkillID = (Label)rptSkill.Items[i].FindControl("lbl_SkillID");

                    string sql = " FKGradeSkillID=" + lbl_SkillID.Text + " and FKGradeContentGuid='" + _contentID + "' and FKGradeUserID=" + uid.ToString();
                    DataTable dt = new DataTable();
                    dt = op.QueryGrade(sql);

                    vo.FKGradeResourceID = 0;
                    vo.FKGradeContentGuid = _contentID;
                    vo.FKGradeSkillID = int.Parse(lbl_SkillID.Text);
                    vo.FKGradeUserID = uid;
                    vo.GradeCreatetime = DateTime.Now;
                    vo.GradeScore = int.Parse(ddl.SelectedItem.Value);

                    if (dt.Rows.Count > 0)
                    {
                        vo.GradeID = int.Parse(dt.Rows[0]["GradeID"].ToString());
                        op.UpdateGrade(vo);
                    }
                    else
                    {
                        op.AddGrade(vo);
                    }
                }
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            catch
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNO", PowerCreator.LMS.Language.LanagetItemType.Page));
            }

        }
        else
        {
            return;
        }


        //try
        //{
        //    for (int i = 0; i < rptSkill.Items.Count; i++)
        //    {
        //        string uid = "10000";
        //        DropDownList ddl = (DropDownList)rptSkill.Items[i].FindControl("ddl_SkillScore");
        //        //Label lbl_SkillScore = (Label)rptSkill.Items[i].FindControl("lbl_SkillScore");
        //        Label lbl_SkillID = (Label)rptSkill.Items[i].FindControl("lbl_SkillID");

        //        string sql = " FKGradeSkillID=" + lbl_SkillID.Text + " and FKGradeResourceID=" + _resourceid + " and FKGradeUserID=" + uid;
        //        DataTable dt = new DataTable();
        //        dt = op.QueryGrade(sql);

        //        vo.FKGradeResourceID = int.Parse(_resourceid);
        //        vo.FKGradeSkillID = int.Parse(lbl_SkillID.Text);
        //        vo.FKGradeUserID = int.Parse(uid);
        //        vo.GradeCreatetime = DateTime.Now;
        //        vo.GradeScore = int.Parse(ddl.SelectedItem.Value);

        //        if (dt.Rows.Count > 0)
        //        {
        //            vo.GradeID = int.Parse(dt.Rows[0]["GradeID"].ToString());
        //            op.UpdateGrade(vo);
        //        }
        //        else
        //        {
        //            op.AddGrade(vo);
        //        }
        //    }
        //}
        //catch
        //{
        //    //Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
        //    return;
        //}

    }


    protected void btnCloseMe_ServerClick(object sender, EventArgs e)
    {
        PowerCreator.LMS.Common.Script.Script.CloseMe();
    }
}

